package com.example.restatr.Atcivity.CitySubway.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.restatr.Atcivity.CitySubway.MetroLineAt;
import com.example.restatr.Atcivity.CitySubway.MetroStepAt;
import com.example.restatr.Atcivity.CitySubway.Bean.MetroStepBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MetroLineFragment extends Fragment {
    private RecyclerView rv;
    private List<MetroStepBean.DataBean> StepBeans = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_metro_line, container, false);
        rv = inflate.findViewById(R.id.rv);
        return inflate;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            GetStepList();
        }
    }

    private void GetStepList() {
        StepBeans.clear();
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/metro/step/list?lineId=" + MetroLineAt.dataBeans.get(MetroLineAt.vp.getCurrentItem()).getLineId(), "");
            MetroStepBean metroStepBean = new Gson().fromJson(get, MetroStepBean.class);
            StepBeans.addAll(metroStepBean.getData());
            if (getActivity() != null) {
                getActivity().runOnUiThread(() -> {
                    if (StepBeans.size() > 0) {
                        rv.setAdapter(new busStopAdapter());
                    }
                });
            }
        }).start();
    }

    class busStopAdapter extends RecyclerView.Adapter<busStopAdapter.bustStopViewHolder> {

        @NonNull
        @Override
        public bustStopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.mileage_item_layout, parent, false);
            return new busStopAdapter.bustStopViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull bustStopViewHolder holder, int position) {
            holder.date.setTextColor(0XFF000000);
            holder.date.setText(StepBeans.get(position).getName());
            holder.amount.setVisibility(View.GONE);
            holder.distance.setVisibility(View.GONE);
            holder.gasFilling.setVisibility(View.GONE);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MetroStepAt.class);
                    intent.putExtra("name",StepBeans.get(position).getName());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return StepBeans.size();
        }

        class bustStopViewHolder extends RecyclerView.ViewHolder {
            private TextView date;
            private TextView distance;
            private TextView gasFilling;
            private TextView amount;

            public bustStopViewHolder(@NonNull View itemView) {
                super(itemView);
                date = itemView.findViewById(R.id.date);
                distance = itemView.findViewById(R.id.distance);
                gasFilling = itemView.findViewById(R.id.gasFilling);
                amount = itemView.findViewById(R.id.amount);
            }
        }
    }

}